package cn.whit.blog.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import cn.whit.blog.dao.VoteDao;
import cn.whit.blog.dao.VoteItemDao;
import cn.whit.blog.domain.User;
import cn.whit.blog.domain.Vote;
import cn.whit.blog.domain.VoteItem;
import cn.whit.blog.util.DynamicQuery;
import cn.whit.blog.util.JDBCUtils;
import cn.whit.blog.util.PageModel;
import cn.whit.blog.util.Parameter;

public class VoteItemDaoImpl implements VoteItemDao {

	@Override
	public VoteItem findById(String id) throws SQLException {
		String sql="select * from vote_item where id=?";
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		return qr.query(sql, new BeanHandler<VoteItem>(VoteItem.class),id);
	}

	@Override
	public List<VoteItem> findAll() throws SQLException {
		String sql="select * from vote_item";
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		return qr.query(sql, new BeanListHandler<VoteItem>(VoteItem.class));
	}


	@Override
	public List<VoteItem> findAllWithPageAndQuery(int startIndex, int pageSize, DynamicQuery query) throws Exception {
		String sql="select * from vote_item where 1=1 ";
		query.setBaseSql(sql);
		sql = query.generateSql();
		sql += " order by id desc limit  ? , ? ";
        //如果想排序，自行在sql后添加   
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		
		ArrayList<Parameter> parameters = query.getParameters();
		Object[] objs = new Object[parameters.size() + 2];
		int i = 0;
		for (Parameter p : parameters) {
			objs[i] = p.getValue();
			i++;
		}
		objs[i] = startIndex;
		objs[i+1] = pageSize;
		return qr.query(sql, new BeanListHandler<VoteItem>(VoteItem.class),objs);
	}

	@Override
	public int findTotalRecordsAndQuery(DynamicQuery query) throws Exception {
		String sql="select count(*) from vote_item where 1=1 ";
		query.setBaseSql(sql);
		sql = query.generateSql();
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		ArrayList<Parameter> parameters = query.getParameters();
		Object[] objs = new Object[parameters.size()];
		int i = 0;
		for (Parameter p : parameters) {
			objs[i] = p.getValue();
			i++;
		}
		Long num=(Long)qr.query(sql, new ScalarHandler(),objs);
		return num.intValue();
	}

	@Override
	public void save(VoteItem voteItem) throws SQLException {
		String sql="INSERT INTO vote_item(voteid,optionid,userid) "
				+ " VALUES(?,?,?)";
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		Object[] params={voteItem.getVoteid(),voteItem.getOptionid(),voteItem.getUserid()};
		qr.update(sql,params);
	}

	@Override
	public void update(VoteItem voteItem) throws SQLException {
		String sql="update vote_item set title= ? ,content=? ,status =? ,options =? ,memo =? , startTime =?  ,endTime =? ,createTime= ? WHERE id=?";
//		Object[] params={vote.getTitle(),vote.getContent(),vote.getStatus(),vote.getOptions(),
//				vote.getMemo(),vote.getStartTime(),vote.getEndTime(),vote.getCreateTime(),vote.getId()};
//		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
//		qr.update(sql,params);
	}

	@Override
	public List<VoteItem> findByUserIdAndVoteId(String id, String voteid) throws SQLException {
		String sql="select * from vote_item where userid = ? and voteid = ? ";
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		Object[] params={id,voteid};
		return qr.query(sql, new BeanListHandler<VoteItem>(VoteItem.class),params);
	}

	@Override
	public void deleteByVoteId(String s) throws SQLException {
		String sql=" delete from vote_item where voteid = ? ";
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		Object[] params={s};
		qr.update(sql,params);
	}


}
